Imports System.Data.SqlClient

Public Class frmCrea_Destajo
    Private mstrCODIGO As String
    Public WriteOnly Property CODIGO() As String

        Set(ByVal value As String)
            mstrCODIGO = value
        End Set
    End Property
    Private SEPARADOR As Char
    Private Elemento1 As New DevComponents.Editors.ComboItem()
    Private Elemento2 As New DevComponents.Editors.ComboItem()
    Private Elemento3 As New DevComponents.Editors.ComboItem()
    Private Sub frmCrea_Destajo_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Elemento1.FontName = "Segoe UI"
        Elemento1.FontName = "Segoe UI"
        Elemento1.FontSize = 10.0!
        Elemento1.FontStyle = System.Drawing.FontStyle.Italic
        Elemento1.ForeColor = System.Drawing.Color.FromArgb(CType(CType(64, Byte), Integer), CType(CType(64, Byte), Integer), CType(CType(64, Byte), Integer))
        Elemento1.Text = " "
        Elemento1.TextAlignment = System.Drawing.StringAlignment.Center

        Elemento2.FontName = "Segoe UI"
        Elemento2.FontName = "Segoe UI"
        Elemento2.FontSize = 10.0!
        Elemento2.FontStyle = System.Drawing.FontStyle.Italic
        Elemento2.ForeColor = System.Drawing.Color.FromArgb(CType(CType(64, Byte), Integer), CType(CType(64, Byte), Integer), CType(CType(64, Byte), Integer))
        Elemento2.Text = "Buscar mas..."
        Elemento2.TextAlignment = System.Drawing.StringAlignment.Center

        Elemento3.FontName = "Segoe UI"
        Elemento3.FontName = "Segoe UI"
        Elemento3.FontSize = 10.0!
        Elemento3.FontStyle = System.Drawing.FontStyle.Italic
        Elemento3.ForeColor = System.Drawing.Color.FromArgb(CType(CType(64, Byte), Integer), CType(CType(64, Byte), Integer), CType(CType(64, Byte), Integer))
        Elemento3.Text = "Registrar nuevo..."
        Elemento3.TextAlignment = System.Drawing.StringAlignment.Center

        Me.BackColor = Color.White
        SEPARADOR = Application.CurrentCulture.NumberFormat.NumberDecimalSeparator

        'lblTitulo.BackColor = System.Drawing.Color.FromArgb(18, 19, 20)
        'lblTitulo.ForeColor = System.Drawing.Color.FromArgb(154, 154, 154)
        'botonesRegresar.BackColor = System.Drawing.Color.FromArgb(18, 19, 20)

        selBuscar.Visible = False
        selCitra.Visible = False
        selNotificaciones.Visible = False
        selPendientes.Visible = False
        selMas.Visible = False

        selBuscar.BackColor = System.Drawing.Color.FromArgb(132, 189, 0)
        selCitra.BackColor = System.Drawing.Color.FromArgb(132, 189, 0)
        selNotificaciones.BackColor = System.Drawing.Color.FromArgb(132, 189, 0)
        selPendientes.BackColor = System.Drawing.Color.FromArgb(132, 189, 0)
        selMas.BackColor = System.Drawing.Color.FromArgb(132, 189, 0)


        'Panel1.BackColor = System.Drawing.Color.FromArgb(34, 35, 38)

        Line1.BackColor = System.Drawing.Color.FromArgb(18, 19, 20)
        Line1.ForeColor = System.Drawing.Color.FromArgb(50, 52, 56)
        ''50, 52, 56
        Line2.ForeColor = System.Drawing.Color.FromArgb(224, 224, 224)


        Line2.BackColor = Color.White
        Line3.BackColor = Color.White
        Line4.BackColor = Color.White
        Line14.BackColor = Color.White

        Line2.ForeColor = System.Drawing.Color.FromArgb(224, 224, 224)
        Line3.ForeColor = System.Drawing.Color.FromArgb(224, 224, 224)
        Line4.ForeColor = System.Drawing.Color.FromArgb(224, 224, 224)
        Line14.ForeColor = System.Drawing.Color.FromArgb(224, 224, 224)

        '132, 189, 0
        selCitra.BackColor = System.Drawing.Color.FromArgb(132, 189, 0)
        selNotificaciones.BackColor = System.Drawing.Color.FromArgb(132, 189, 0)
        selPendientes.BackColor = System.Drawing.Color.FromArgb(132, 189, 0)
        selBuscar.BackColor = System.Drawing.Color.FromArgb(132, 189, 0)
        selMas.BackColor = System.Drawing.Color.FromArgb(132, 189, 0)

        selCitra.Visible = False
        selNotificaciones.Visible = False
        selPendientes.Visible = False
        selBuscar.Visible = False
        selMas.Visible = False

        lblMaterial.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64)
        lblActivo.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64)
        lblTipoMat.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64)
        lblDescProv.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64)
        lblTipoMat.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64)

        If PARAMETROS.ENTRADA_dATOS = 1 Then
            txtNombre.CharacterCasing = CharacterCasing.Upper
        ElseIf PARAMETROS.ENTRADA_dATOS = 2 Then
            txtNombre.CharacterCasing = CharacterCasing.Lower
        ElseIf PARAMETROS.ENTRADA_dATOS = 3 Then
            'FORMAT_CULTURA
        ElseIf PARAMETROS.ENTRADA_dATOS = 0 Then
            txtNombre.CharacterCasing = CharacterCasing.Normal
        End If

        '***********************************************************
        '/////////////// COMIENZA CARGA DE LISTAS

        CargaMaquinas()


        Actualizar()

        If mstrCODIGO = "" Then
            btnTodosRegresar.Enabled = False
            Me.WindowState = FormWindowState.Normal
        Else
            btnTodosRegresar.Enabled = True
            Me.WindowState = FormWindowState.Maximized
        End If



    End Sub
    Sub CargaDepartamentos(ByVal FILAS As Integer, ByVal IDS As String)
        Dim strSql As String = ""
        Dim strNombre As String = ""

        strSql = "SELECT TOP " & FILAS & " NOMBRE FROM DEPARTAMENTOS WHERE CLAVE NOT IN (" & IDS & ") AND ESTATUS=1 AND ES_PRODUCCION=1 ORDER BY CLAVE DESC"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        Try
            rdBuscar = cmdBuscar.ExecuteReader

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso Unidades")
        End Try

        Do While rdBuscar.Read()
            lstDepartamento.Items.Add(rdBuscar("NOMBRE"))
        Loop

        lstDepartamento.Items.Add(Elemento1)
        lstDepartamento.Items.Add(Elemento2)

    End Sub
    Sub CargaDepartamentos_Usados()
        lstDepartamento.Items.Clear()

        Dim strSql As String = ""
        Dim strNombre As String = ""

        strSql = "SELECT TOP 7 C.CLAVE_DEPTO,COUNT(C.CLAVE_DEPTO) AS REPETICIONES FROM DEPARTAMENTOS D INNER JOIN OPERACIONES C ON C.CLAVE_DEPTO=D.CLAVE WHERE D.ESTATUS=1 AND ES_PRODUCCION=1 GROUP BY C.CLAVE_DEPTO HAVING COUNT(M.CLAVE_DEPTO)>0 ORDER BY COUNT(M.CLAVE_DEPTO)"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        Try
            rdBuscar = cmdBuscar.ExecuteReader

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso Departamentos")
        End Try
        Dim CONT As Integer = 0

        Dim CadenaIds As String = ""

        Do While rdBuscar.Read()
            lstDepartamento.Items.Add(BuscarStrDatoCitra("SELECT NOMBRE FROM DEPARTAMENTOS WHERE CLAVE='" & rdBuscar("CLAVE_DEPTO") & "'"))
            CONT = CONT + 1
            CadenaIds = CadenaIds & "," & rdBuscar("CLAVE_DEPTO")
            If CONT = 7 Then
                Exit Do
            End If
        Loop

        If CONT = 0 Then
            CadenaIds = ",0"
        End If


        If CONT < 7 Then
            CargaDepartamentos(7 - CONT, Mid(CadenaIds, 2))
        Else
            lstDepartamento.Items.Add(Elemento1)
            lstDepartamento.Items.Add(Elemento2)
        End If
    End Sub
    Sub CargaMaquinas()
        lstMaquina.Items.Clear()

        Dim strSql As String = ""
        Dim strNombre As String = ""

        strSql = "SELECT TOP 7 NOMBRE FROM MAQUINARIA WHERE ESTATUS=1 ORDER BY IDMAQUINARIA DESC"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        Try
            rdBuscar = cmdBuscar.ExecuteReader
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso Unidades")
        End Try

        Do While rdBuscar.Read()
            lstMaquina.Items.Add(rdBuscar("NOMBRE"))
        Loop

        lstMaquina.Items.Add(Elemento1)
        lstMaquina.Items.Add(Elemento2)
        lstMaquina.Items.Add(Elemento3)
    End Sub
    Sub Actualizar()
        CargaDepartamentos_Usados()

        Dim strSql As String = ""
        strSql = "SELECT * FROM OPERACIONES WHERE UPPER(CLAVE)='" & mstrCODIGO & "'"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection
        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        Try
            rdBuscar = cmdBuscar.ExecuteReader

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        End Try

        If rdBuscar.Read() Then
            txtCodes.Text = rdBuscar("CLAVE")
            txtNombre.Text = rdBuscar("NOMBRE")
            lstDepartamento.Text = rdBuscar("DEPARTAMENTO")
            lstMaquina.Text = rdBuscar("MAQUINA")

            If rdBuscar("ESTATUS") = 1 Then
                chActivo.Checked = True
            Else
                chActivo.Checked = False
            End If

            txtCapacidad.Text = rdBuscar("CAPACIDAD")
            txtUnidadesxHora.Text = rdBuscar("PIEZA_HORA")
            txtTiempo.Text = rdBuscar("HORAS_PIEZA")
            txtCostos.Text = rdBuscar("COSTO_FIJO")
            txtVariable.Text = rdBuscar("COSTO_VAR")
            txtHoras.Text = rdBuscar("CODIGO_SAE")

           

            ' dgProveedores.Rows.Clear()
            BLOQUEAR_ELEMENTOS()
        Else
            DESBLOQUEAR_ELEMENTOS()

            txtCodes.Text = ""
            txtNombre.Text = ""
            lstDepartamento.Text = ""
            lstMaquina.Text = ""

            chActivo.Checked = True
           

            txtCapacidad.Text = ""
            txtUnidadesxHora.Text = ""
            txtTiempo.Text = ""
            txtCostos.Text = ""
            txtVariable.Text = ""
            txtHoras.Text = ""

        End If

        txtCodes.Focus()

        txtCodes.Select()
        Application.DoEvents()

    End Sub
    Sub DESBLOQUEAR_ELEMENTOS()
        txtCodes.Enabled = True
        txtNombre.Enabled = True
        lstDepartamento.Enabled = True
        chActivo.Enabled = True
        txtCapacidad.Enabled = True
        txtCostos.Enabled = True
        txtHoras.Enabled = True
        txtTiempo.Enabled = True
        txtUnidadesxHora.Enabled = True
        txtVariable.Enabled = True
        
    End Sub
    Sub BLOQUEAR_ELEMENTOS()
        txtCodes.Enabled = False
        txtNombre.Enabled = False
        lstDepartamento.Enabled = False
        chActivo.Enabled = False
        txtCapacidad.Enabled = False
        txtCostos.Enabled = False
        txtHoras.Enabled = False
        txtTiempo.Enabled = False
        txtUnidadesxHora.Enabled = False
        txtVariable.Enabled = False

    End Sub

    Private Sub ButtonItem3_Click(sender As System.Object, e As System.EventArgs) Handles ButtonItem3.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Close()
    End Sub

    Private Sub lstDepartamento_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstDepartamento.SelectedIndexChanged

    End Sub
End Class